package com.example.ying.common;

import com.example.ying.common.filter.exception.DefinitionException;
import lombok.Data;

/**
 * @Author DarkMastar
 * @Time : 2023/11/24 19:04
 **/
@Data
public class R {

    private int code;

    private String message;

    private String type;

    private Boolean success;

    private Object data;

    public static R success(String message) {
        R r = new R();
        r.setCode(200);
        r.setMessage(message);
        r.setSuccess(true);
        r.setType("success");
        r.setData(null);
        return r;
    }

    public static R success(String message, Object data) {
        R r = success(message);
        r.setData(data);
        return r;
    }

    //自定义异常返回的结果
    public static R defineError(DefinitionException de){
        R result = new R();
        result.setSuccess(false);
        result.setCode(de.getErrorCode());
        result.setMessage(de.getErrorMsg());
        result.setData(null);
        return result;
    }
    //其他异常处理方法返回的结果
    public static R otherError(Integer code, String message) {
        R result = new R();
        result.setSuccess(false);
        result.setCode(code);
        result.setMessage(message);
        result.setType("error");
        return result;
    }

    public static R error(String message) {
        R r = new R();
        r.setMessage(message);
        r.setSuccess(false);
        r.setType("error");
        r.setData(null);
        return r;
    }

    public static R warning(String message) {
        R r = new R();
        r.setMessage(message);
        r.setSuccess(false);
        r.setType("warning");
        r.setData(null);
        return r;
    }

    public static R fatal(String message) {
        return null;
    }
}
